
import moduleView from '../models/movie'
import moviecenterView from '../views/movie-center.art'
import movielistView from '../views/movie-list.art'
import cityView from '../views/city.art'

const  lodash= require('lodash')
const BScroll = require('better-scroll');

class movie{
constructor(){  
   this.id=[]
   this.list=[]
   this.index=0
   this.cityId=1
}

  renderer(list){
let  movielistHtml=movielistView({
    list
})
$('main .list-container ul').html(movielistHtml)
  $('main .list-container ul li').on('tap',function(){
   let id=$(this).attr('data-id')
    location.hash=`detialJs/${id}`
  })
 
}

 async  render(){
   let that=this
    let result=await moduleView.get({})
   // console.log(result)
    let  moviecenterhtml=moviecenterView({})
    $('main').html(moviecenterhtml)
       this.cityrender()
    let id=this.id=lodash.chunk(result.movieIds,12)

   //  console.log(id)
    let list=this.list=this.handleurl(result)
    this.renderer(list)
    
     let bScroll = new BScroll.default($('main').get(0), {
         probeType: 2
       })
    
        bScroll.scrollBy(0, 0)

   bScroll.on('scrollEnd', async function(){ 
     if(this.maxScrollY >= this.y && that.id.length-1 > that.index){
        that.index++
      let str=that.id[that.index].join('%2C')
    
        console.log(str) 
 let result=await moduleView.coming(str)
    console.log(result)
    let list=that.handlecomingurl(result)
  //   console.log(list)
      that.list=[...that.list,...list]
  //    console.log(that.list)
      that.renderer(that.list)
      bScroll.scrollBy(0, 0)
      }
    })

        
         


}

handleurl(result){
  let re=/w.h/g
  let  len=result.movieList.length
    for(let i=0;i<len;i++){
      result.movieList[i].img=result.movieList[i].img.replace(re, '128.180')
     if(result.movieList[i].showst==3){
       result.movieList[i].showst='购票'
     }else if(result.movieList[i].showst==4){
       result.movieList[i].showst='预售'
     }
}
    let list=this.list=result.movieList
    return list

}

handlecomingurl(result){
  let re=/w.h/g
  let  len=result.coming.length
    for(let i=0;i<len;i++){
      result.coming[i].img=result.coming[i].img.replace(re, '128.180')
     if(result.coming[i].showst==3){
       result.coming[i].showst='购票'
     }else if(result.coming[i].showst==4){
       result.coming[i].showst='预售'
     }
}
    let list=result.coming
    return list
}

cityrender(){ 
  let that=this
  $('.position').on('tap',async function(){
    
    let data=await moduleView.getcity() 
     that.sortCity(data.cts)
     let list=data.cts
   //  console.log(data.cts)  
       let html=cityView({
         list
       })
  $('.city').html(html)
  $('.city').css('display','block')
  that.cathref()
  }) 
 
}

sortCity(arr){
  function chineseLetter (arr, dataLeven) {
    var letter = 'abcdefghjklmnopqrstwxyz'.split('')
    var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('')
    /* 获取数组元素比较的值 */
    function getValue (option) {
      if (!dataLeven) return option
      var data = option
      dataLeven.split('.').filter(function (item) {
        data = data[item]
      })
      return data + ''
    }
    /* 进行排序 */
    arr.sort(function (item1, item2) {
      return getValue(item1).localeCompare(getValue(item2), 'zh-Hans-CN')
    })
    /* 判断需要排序的字符串是否含有中文字符 */
    if (/[\u4e00-\u9fff]/.test(getValue(arr[0])) && typeof arr[0] === 'object') pySegSort(0, 0)
    /* 给省列表中添加首字符 */
    function pySegSort (letterIndex, zhIndex) {
      var first = true // 首次是否加 字母标识
      for (var i = zhIndex; i < arr.length; i++) {
        var item = arr[i]
        //      是否有值 && 当前值大于等于本次字母的最小值 && (最后一位 || 当前值小于下次字母的最小值)
        var state = zh[letterIndex] && getValue(item).localeCompare(zh[letterIndex], 'zh') >= 0 && (letterIndex === letter.length - 1 || getValue(item).localeCompare(zh[letterIndex+1], 'zh') < 0)
        if (state) { // 满足条件，同一个首字母下的：例如 A 下的所有省份
          if (first) { //是否是第一次出现
            item.letter = letter[letterIndex].toUpperCase()
            first = false
          } else {
            item.letter = ''
          }
        } else { // 递归调用 函数，进行下次字母下的排列
          letterIndex++
          if (letterIndex < letter.length) {
            pySegSort(letterIndex, i)
            break
          }
        }
      }
    }
  }  
  chineseLetter(arr, 'nm')
//console.log(arr)
}

cathref(){
  let that=this
  $('.city-box ul li').on('tap',function(){
  let html=$(this).html()
  window.localStorage.city=html
  console.log(window.localStorage.city)
  let cityId=that.cityId=$(this).attr('data-id') 
  // console.log(cityId) 
 // console.log(that.cityId)
 $('.city').css('display',"none")
 $('.position').html($(this).html())
  })

 return that.cityId

}


}

 export default   new movie() 